#include "editeur.h"

#if 1

static int _ed_cur_level;

void _editeur_change_level(int new_level);

void _editeur_change_level(int new_level)
{
    if(new_level < 1)
    {
        new_level = 1;
    }
    game_save_level(_ed_cur_level);

    _ed_cur_level = new_level;
    game_load_level(_ed_cur_level);
}

void editeur_start(void)
{
    int adding_obstacle = 0;
    Obstacle *obstacle = 0;

    gfx_scene_begin();
    game_set_nb_of_players(4);
    game_init();

    _ed_cur_level = 1;
    game_load_level(1);

    const char* help[] =
    {
        "EDITOR :",
        "--------",
        "Left click to put a wall",
        "Right click to remove a wall",
        "Up     : next level",
        "Down   : previous level",
        "Escape : save and quit",
        0
    } ;



    while(1)
    {
        gfx_scene_begin();
        game_draw();
        char text[16];
        sprintf(text, "Level %d", _ed_cur_level);

        gfx_draw_text(380, 80, text);
        for(unsigned i = 0; help[i]; i++)
        {
            gfx_draw_text(380, 100+i*16, help[i]);
        }

        // Keyboard input
        io_player_read_buttons();
        int p1state = io_player_get_buttons(0);
        if(p1state & IO_PLAYER_TOP)
        {
            if(adding_obstacle)
            {
               adding_obstacle = 0;
               obstacle->in_use = 0;
            }
            _editeur_change_level(_ed_cur_level+1);
            time_sleep(0.2);
        }
        else if(p1state & IO_PLAYER_BOTTOM)
        {
            if(adding_obstacle)
            {
               adding_obstacle = 0;
               obstacle->in_use = 0;
            }
            _editeur_change_level(_ed_cur_level-1);
            time_sleep(0.2);
        }
        else if(p1state & IO_PLAYER_PAUSE)
        {
            adding_obstacle = 0;
            game_save_level(_ed_cur_level);
            return;
        }

        // Track current mouse position for second point
        float mx, my;
        int mbuttons;
        io_get_mouse(&mx, &my, &mbuttons);

        if(adding_obstacle == 1)
        {
            obstacle->p2[0] = mx;
            obstacle->p2[1] = my;
        }

        // Add obstacle
        if((mbuttons & IO_MOUSE_LBUTTON) && adding_obstacle == 0)
        {
            // Start adding obstacle
            // Find free obstacle
            for(int i = 0; ; i++)
            {
                obstacle = game_get_obstacle(i);
                if(obstacle->in_use == 0)
                {
                    obstacle->in_use = 1;
                    obstacle->color = GFX_WHITE;
                    break;
                }
            }
            // Add first point
            obstacle->p1[0] = mx;
            obstacle->p1[1] = my;
            adding_obstacle = 1;
        }
        else if(!(mbuttons & IO_MOUSE_LBUTTON) && adding_obstacle == 1)
        {
            adding_obstacle = 0;
        }

        // Remove obstacle
        else if(mbuttons & IO_MOUSE_RBUTTON)
        {
            if(adding_obstacle == 1)
            {
                obstacle->in_use = 0;
                adding_obstacle = 0;

            }
            else
            {
                Ball ball;
                ball.radius = 5;
                ball.position[0] = mx;
                ball.position[1] = my;
                for(int i = 0; i < OBSTACLE_MAX; i++)
                {
                    Obstacle *obs_test = game_get_obstacle(i);
                    vec2 dumb_a, dumb_b;
                    if(game_collide_segment_ball(obs_test->p1, obs_test->p2, &ball, dumb_a, dumb_b))
                    {
                        obs_test->in_use = 0;
                    }
                }
            }
        }
        gfx_scene_end();
    }
}

#endif
